Scanner assignment
Description
Write the scanner, the first phase of your compiler. The scanner
reads in all tokens of an input program and generates an output program
with some tokens modified. Both input and output program are correct
C programs. The specific requirements for code generation are:
-
The scanner should copy any source line started with "#" to the beginning
of the generated program. These are "meta-statements" that are outside
of the language we are studying. You only need to copy them without any
other processing. All such lines appears at the beginning of the
input program and should be placed at the beginning of the output program.
-
The scanner should recognize all the tokens of the input program.
The
token definitions are here.
-
The scanner to add string "cs254" to the beginning of every ID except
the name of function "main".
-
The scanner takes an input program name and puts the generated program
in a new file named with an extension "_cs254". So "foo.c" will become
"foo_cs254.c".
-
The output program does not need to be formatted in any way. The
scanner can write all statements in one line, although the meta-statements
should be copied line by line.
For example, the following program should be converted
as follows.
example program : foo.c
#include <stdio.h>
#define read(x) scanf("%d\n", &x)
#define write(x) printf("%d\n", x)
void foo() {
int a;
read(a);
write(a);
}
int main() {
foo();
}
running your scanner:
% scanner foo.c
generated program (no formatting needed): foo_cs254.c
#include <stdio.h>
#define read(x) scanf("%d\n", &x)
#define write(x) printf("%d\n", x)
void cs254foo() {
int cs254a;
read(cs254a);
write(cs254a);
}
int main() {
cs254foo();
}
The name change should not change the result of the program. You
can test the correctness of your scanner by comparing the execution result
of a program before and after the name change.
Recommended interface of the scanner object:
-
Initialization of the scanner with the program_file_name.
-
Boolean HasMoreTokens(): returns true if there is any token left in the
program.
-
Token CurrentToken(): read the content of the current token while still
keeping the token in the scanner.
-
void MoveToNextToken(): pop up the current token and move to the next token.
With this interface, you can scan through and print a program as follows:
Scanner scan1("test1.c"); // Copy meta-statements.
Initialize the scanner.
While (scan1.HasMoreTokens()) {
print(scan1.CurrentToken());
// If it is an ID, add cs254 to its name (except for "main").
scan1.MoveToNextToken();
}
Test programs
A list of 8 standard test programs can be downloaded
from here. Scroll down to see the content of README.